package com.yuanqi.service.impl;

import com.yuanqi.domain.ManualPayment;
import com.yuanqi.repository.ManualPaymentRepository;
import com.yuanqi.service.ManualPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ManualPaymentServiceImpl implements ManualPaymentService {
    
    @Autowired
    private ManualPaymentRepository manualPaymentRepository;
    
    @Override
    public ManualPayment createPayment(ManualPayment payment) {
        // 生成付款编号
        if (payment.getPaymentCode() == null || payment.getPaymentCode().isEmpty()) {
            payment.setPaymentCode(generatePaymentCode());
        }
        
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        payment.setCreateTime(now);
        payment.setUpdateTime(now);
        
        // 设置默认状态
        if (payment.getStatus() == null) {
            payment.setStatus(0); // 0-待付款
        }
        
        return manualPaymentRepository.save(payment);
    }
    
    @Override
    public ManualPayment updatePayment(ManualPayment payment) {
        ManualPayment existingPayment = manualPaymentRepository.findById(payment.getId())
                .orElseThrow(() -> new RuntimeException("手动付款记录不存在"));
        
        // 更新字段
        existingPayment.setPaymentType(payment.getPaymentType());
        existingPayment.setSupplierId(payment.getSupplierId());
        existingPayment.setSupplierName(payment.getSupplierName());
        existingPayment.setPaymentAmount(payment.getPaymentAmount());
        existingPayment.setPaymentMethod(payment.getPaymentMethod());
        existingPayment.setPaymentDate(payment.getPaymentDate());
        existingPayment.setRemark(payment.getRemark());
        existingPayment.setStatus(payment.getStatus());
        existingPayment.setUpdateTime(LocalDateTime.now());
        existingPayment.setUpdater(payment.getUpdater());
        
        return manualPaymentRepository.save(existingPayment);
    }
    
    @Override
    public void deletePayment(Long id) {
        manualPaymentRepository.deleteById(id);
    }
    
    @Override
    public ManualPayment findById(Long id) {
        return manualPaymentRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("手动付款记录不存在"));
    }
    
    @Override
    public ManualPayment findByPaymentCode(String paymentCode) {
        return manualPaymentRepository.findByPaymentCode(paymentCode)
                .orElseThrow(() -> new RuntimeException("手动付款记录不存在"));
    }
    
    @Override
    public List<ManualPayment> findAll() {
        return manualPaymentRepository.findAll();
    }
    
    @Override
    public List<ManualPayment> findBySupplierId(Long supplierId) {
        return manualPaymentRepository.findBySupplierId(supplierId);
    }
    
    @Override
    public List<ManualPayment> findByStatus(Integer status) {
        return manualPaymentRepository.findByStatus(status);
    }
    
    @Override
    public String generatePaymentCode() {
        Integer maxNumber = manualPaymentRepository.findMaxPaymentCodeNumber();
        int nextNumber = (maxNumber != null ? maxNumber : 0) + 1;
        return String.format("MP%08d", nextNumber);
    }
    
    @Override
    public Map<String, Object> getStatistics() {
        Map<String, Object> stats = new HashMap<>();
        
        // 统计总数
        long totalCount = manualPaymentRepository.count();
        stats.put("totalCount", totalCount);
        
        // 统计各状态数量
        long pendingCount = manualPaymentRepository.countByStatus(0);
        long paidCount = manualPaymentRepository.countByStatus(1);
        long cancelledCount = manualPaymentRepository.countByStatus(2);
        
        stats.put("pendingCount", pendingCount);
        stats.put("paidCount", paidCount);
        stats.put("cancelledCount", cancelledCount);
        
        return stats;
    }
}

